﻿using System;
using System.Web.UI;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using System.Web.UI.WebControls;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.DataAccess.DataObjects.Enums;
using Web.Codes;
using Web.Utils;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class PositionEntry : BasePage
    {
        [Inject]
        public IPositionService _positionService { get; set; }

        public bool IsSuccess { get; set; }
        public bool FoundError { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _positionService.GetAll();
            repeater.DataBind();
        }

        void Delete(int id)
        {
            _positionService.Delete(id);
            IsSuccess = _positionService.Save();
            FoundError = !IsSuccess;

            if (!IsSuccess)
            {
                NotifyText = "Cannot be deleted, found error or there is a dependency on this record.";
            }
            else {
                NotifyText = "Successfully Deleted.";
            }
            
            BindGrid();
        }

        void Edit(int id)
        {
            var pos = _positionService.GetById(id);

            ObjectToData(pos);

            SubmitBtn.Text = "Update";
        }
        void Update(int id)
        {
            var pos = _positionService.GetById(id);
            if (pos == null)
            {
                Add();
                return;
            }
            DataToObject(ref pos);

            _positionService.Update(pos);
            IsSuccess = _positionService.Save();
            NotifyText = "Successfully Updated Record.";
            ClearForm();
            BindGrid();
        }

        void Add()
        {
            var pos = new Position();

            DataToObject(ref pos);

            _positionService.Add(pos);
            IsSuccess = _positionService.Save();
            NotifyText = "Successfully Added New Record.";
            BindGrid();
        }
        void DataToObject(ref Position obj)
        {
            double dblIncentive;
            double dblRetirement;
            double dblUniformAllowance;

            obj.Code = CodeTxt.Text;
            obj.Description = DescTxt.Text;

            double.TryParse(IncentiveTxt.Text, out dblIncentive);
            double.TryParse(RetirementTxt.Text, out dblRetirement);
            double.TryParse(UniformAllowanceTxt.Text, out dblUniformAllowance);

            obj.Incentive = dblIncentive;
            obj.Retirement = dblRetirement;
            obj.UniformAllowance = dblUniformAllowance;
        }
        void ObjectToData(Position obj)
        {
            PositionId.Value = obj.Id.ToString();
            CodeTxt.Text = obj.Code;
            DescTxt.Text = obj.Description;
            IncentiveTxt.Text = obj.Incentive.ToString();
            RetirementTxt.Text = obj.Retirement.ToString();
            UniformAllowanceTxt.Text = obj.UniformAllowance.ToString();
        }
        void Save()
        {
            int Id;
            int.TryParse(PositionId.Value, out Id);
            if (Id == 0)
                Add();
            else
                Update(Id);
        }

        void ClearForm()
        {
            ClearControl(this.Page);
            PositionId.Value = "0";
            SubmitBtn.Text = "Submit";
        }
        void ClearControl(Control ctrl)
        {
            foreach (Control _ctrl in ctrl.Controls)
            {
                if (_ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)_ctrl).Text = string.Empty;
                }
                else if (_ctrl.GetType() == typeof(DropDownList))
                {
                    ((DropDownList)_ctrl).SelectedIndex = 0;
                }
                ClearControl(_ctrl);
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void AddNewBtn_Click(object sender, EventArgs e)
        {
            Save();
            if (IsSuccess)
            {
                ClearForm();
                BindGrid();
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }
    }
}